<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Book extends Model
{
    protected $table = 'book';
//    private static $suffix;
//    public function __construct($suffix)
//    {
////        self::$suffix = $suffix;
////        if(empty(self::$suffix)){
////            $this->table= 'book_'.date("Y");
////        }else{
////            $this->table= 'book_'.self::$suffix;
////        }
//        $this->table= 'book_'.$suffix;
//        parent::__construct();
//    }
    //关联书籍详情
    public function info()
    {
        return $this->hasOne('App\Models\BookInfo','book_id','id');
    }

    //获取书籍列表
    public function getBookListPage(array $where)
    {
        return DB::table($this->table)->where($where)->paginate();
    }
    //处理搜索条件
    public static function executeSearch($request)
    {
        $where = [];
        $year = $request->year ?? null;
        $month = $request->month ?? null;
        $name = $request->name ?? null;
        $author = $request->author ?? null;
        $source = $request->source ?? null;
        $cate_id = $request->cate_id ?? null;
        if(!empty($year)) $where[] = ['year','=',$year];
        if(!empty($month)) $where[] = ['month','=',$month];
        if(!empty($name)) $where[] = ['name','like',$name.'%'];
        if(!empty($author)) $where[] = ['author','like',$author,'%'];
        if(!empty($source)) $where[] = ['source','like',$source.'%'];
        if(!empty($cate_id)) $where[] = ['cate_id','=',$cate_id];
        return $where;
    }

    //处理 书籍的更新 添加操作
    public static function executeInsert($request)
    {
        $name = $request->name ?? null;
        $author = $request->author ?? null;
        $cate_id = $request->cate_id ?? 0;
        $cate_name = $request->cate_name ?? "";
        $descr = $request->descr ?? "";
        $is_vip = $request->is_vip ?? 0;
        $is_free = $request->is_free ?? 0;
        $source = $request->source ?? '';
        $status = $request->status ?? 1;
        $type = BookCate::query()->where('pid',$cate_id)->value("id") ?? 1;
        return [
            'name' => $name,
            'author' => $author,
            'cate_id' => $cate_id,
            'cate_name' => $cate_name,
            'descr' => $descr,
            'is_vip' => $is_vip,
            'is_free' => $is_free,
            'source' => $source,
            'status' => $status,
            'type' => $type
        ];
    }
    public static function executeUpdate($request)
    {
        $name = $request->name ?? null;
        $author = $request->author ?? null;
        $cate_id = $request->cate_id ?? null;
        $cate_name = $request->cate_name ?? null;
        $descr = $request->descr ?? null;
        $is_vip = $request->is_vip ?? null;
        $is_free = $request->is_free ?? null;
        $source = $request->source ?? null;
        $status = $request->status ?? null;
        if(!is_null($cate_id)){
            $type = BookCate::query()->where('pid',$cate_id)->value("id") ?? 1;
        }
        return [
            'name' => $name,
            'author' => $author,
            'cate_id' => $cate_id,
            'cate_name' => $cate_name,
            'descr' => $descr,
            'is_vip' => $is_vip,
            'is_free' => $is_free,
            'source' => $source,
            'status' => $status,
            'type' => $type ?? null
        ];
    }
    //上传小说文件拼装成数据
    public function fileCoverData($path)
    {
        //
        $content = '';
        $chapter = '';
        $chapter_list = [];
        $content_list = [];
        $j = 0;
        $i = 0;

        $file = fopen($path,"r");
        while(true) {
            if(feof($file)){
                if($content == '' && $chapter != '') break;
                $content_list[] = $content;
                break;
            }else{
                $now = mb_convert_encoding(fgets($file),'UTF-8','UTF-8,GBK,GB2312,BIG5');//使用该函数对结果进行转码
                $start_chapter = strpos($now,'第');
                if((is_int($start_chapter) && is_int(strpos($now,'章'))) || (is_int($start_chapter) && is_int(strpos($now,'节'))) || (is_int($start_chapter) && is_int(strpos($now,'回'))) || (is_int($start_chapter) && is_int(strpos($now,'篇')))){
                    if($content =='' && $chapter ==''){
                        $content .= $now;
                        $chapter = strtr($now,["\r\n" => '']);
                    }else{
                        $content_list[] = $content;
                        $chapter = strtr($now,["\r\n" => '']);
                        $content = '';
                        $j ++;
                    }
                    $chapter_list[] = $chapter;
                }else{
                    $content .= $now;
                }
            }
            $i ++;
        }
        return [
            'chapter_list' => $chapter_list,
            'content_list' => $content_list,
        ];
    }
}
